<?php
namespace system\model;
use houdunwang\model\Model;
class Admin extends model{
//操作的数据表
	protected $table = "admin";
	//登录
	public function login(){
		//1.验证数据不能为空
		$res = Validate::make( [
			//字段、验证规则、提示消息、验证条件,
			[ 'admin_username', 'isnull', '用户名不能为空', self::MUST_VALIDATE ],
			[ 'admin_password', 'isnull', '密码不能为空', self::MUST_VALIDATE ],
			[ 'code', 'isnull', '验证码不能为空', self::MUST_VALIDATE ]
		] );
		if($res===false){
			return ['valid'=>0,'msg'=>Validate::getError()];
		}
		//2.验证用户名和密码不能为空
		$userInfo = $this->where('admin_username',$_POST['admin_username'])->where('admin_password', Crypt::encrypt($_POST['admin_password']))->first();
//		dd($userInfo);
		if(!$userInfo)
		{
			//用户名密码不对
			return ['valid'=>0,'msg'=>'用户名或者密码不正确'];
		}
		//3.验证验证码不能为空
		if(strtolower(Code::get())!=strtolower($_POST['code']))
		{
			return ['valid'=>0,'msg'=>'验证码不正确'];
		}
		//4.验证好的存在session中
		Session::set('admin.admin_id',$userInfo['admin_id']);
		Session::set('admin.admin_username',$userInfo['admin_username']);
		//成功提示
		return ['valid'=>1,'msg'=>'登录成功'];
	}
}



